﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace YZH.Audio.Disting
{

    /// <summary>
    ///
    /// 作    者：叶志辉
    /// 创建时间：
    /// 修改时间：
    ///</summary>   
    internal class VoiceDistingAPI
    {
        #region 字段
        internal const int MAX_NBEST = 20;
        const string dllStr = "asrdec.dll";
        #endregion

        #region 类型
        [StructLayout(LayoutKind.Sequential)]
        internal struct MultiResult
        {
            public int nResult;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAX_NBEST)]
            public byte[] NBestResultStr;
        }

        /// <summary>
        /// 返回值
        /// </summary>
        internal enum RecognizerCode
        {
            RECOGNIZER_RESULT = 1,    //
            RECOGNIZER_OK = 0,    //
            RECOGNIZER_ERROR = -1,   //
            RECOGNIZER_BAD_STATE = -2,//
            RECOGNIZER_BAD_POINTER = -3,   //
            RECOGNIZER_BAD_DATA = -4,   //
            RECOGNIZER_NO_MEMORY = -5,   //
            RECOGNIZER_SPEECH_EXCEED_MAX_LENGTH = -6,
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct DecSentence
        {
            public float CmScore;    // Confidence measure score
            public float Score;      // Reserved, maybe am score or propability
            public int TimeStamp;    // ms
            public int NWords;       // number of words
            public IntPtr Word;        // word sequence
            public IntPtr WordTimeStamp;   // ms
            public IntPtr WordCMScore;   // word CM score
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct DecResult
        {
            public int DecTime;     //花费时间
            public int PcmLength;   //时间
            public int NSentence;
            public IntPtr PSentence;
        }
        #endregion

        #region 调用接口
        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_createBase(out IntPtr baseHandler, string szConfig);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void recognizer_destroyBase(IntPtr baseHandler);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_createSession(out IntPtr sessionHandler, IntPtr baseHandler, string szConfig);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_stopSession(IntPtr sessionHandler);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_startSession(IntPtr sessionHandler, uint resultParam);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void recognizer_destroySession(IntPtr sessionHandler);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_resumeSession(IntPtr sessionHandler, IntPtr data, uint len);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_getSessionRes(IntPtr sessionHandler, IntPtr data, uint len);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern int recognizer_getSessionRes(IntPtr sessionHandler, out IntPtr decResult);

        [DllImport(dllStr, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        public static extern void recognizer_releaseSessionRes(IntPtr decResult);

        #endregion
    }
}
